<?php
if (!defined('ABSPATH')) {
    exit;
}

// 定义主题版本
define('REDDIT_CLONE_VERSION', '1.0.0');

/**
 * 主题设置
 */
function reddit_clone_setup() {
    // 添加主题支持
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('automatic-feed-links');
    add_theme_support('html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
    ));
    
    // 注册导航菜单
    register_nav_menus(array(
        'primary' => __('主导航', 'reddit-clone'),
        'footer' => __('页脚菜单', 'reddit-clone')
    ));

    // 添加自定义文章类型 - 子版块(Subreddit)
    register_post_type('subreddit', array(
        'labels' => array(
            'name' => __('子版块', 'reddit-clone'),
            'singular_name' => __('子版块', 'reddit-clone')
        ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'thumbnail'),
        'menu_icon' => 'dashicons-groups'
    ));
}
add_action('after_setup_theme', 'reddit_clone_setup');

/**
 * 注册小工具区域
 */
function reddit_clone_widgets_init() {
    register_sidebar(array(
        'name' => __('侧边栏', 'reddit-clone'),
        'id' => 'sidebar-1',
        'description' => __('添加小工具到侧边栏', 'reddit-clone'),
        'before_widget' => '<div class="bg-white rounded-lg shadow mb-4 p-4">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="text-lg font-semibold mb-3">',
        'after_title' => '</h3>'
    ));
}
add_action('widgets_init', 'reddit_clone_widgets_init');

/**
 * 投票功能
 */
function reddit_clone_vote() {
    check_ajax_referer('reddit_clone_vote', 'nonce');
    
    $post_id = intval($_POST['post_id']);
    $vote_type = sanitize_text_field($_POST['vote_type']);
    $user_id = get_current_user_id();
    
    if (!$user_id) {
        wp_send_json_error('请先登录');
    }
    
    $votes = get_post_meta($post_id, 'votes', true) ?: 0;
    $user_votes = get_user_meta($user_id, 'user_votes', true) ?: array();
    
    if ($vote_type === 'upvote') {
        $votes++;
        $user_votes[$post_id] = 'up';
    } else {
        $votes--;
        $user_votes[$post_id] = 'down';
    }
    
    update_post_meta($post_id, 'votes', $votes);
    update_user_meta($user_id, 'user_votes', $user_votes);
    
    wp_send_json_success(array('votes' => $votes));
}
add_action('wp_ajax_reddit_clone_vote', 'reddit_clone_vote');

/**
 * 获取帖子评分
 */
function get_post_score($post_id = null) {
    if (!$post_id) {
        $post_id = get_the_ID();
    }
    return intval(get_post_meta($post_id, 'votes', true));
}

/**
 * 获取用户投票状态
 */
function get_user_vote_status($post_id) {
    $user_id = get_current_user_id();
    if (!$user_id) return false;
    
    $user_votes = get_user_meta($user_id, 'user_votes', true) ?: array();
    return isset($user_votes[$post_id]) ? $user_votes[$post_id] : false;
}

/**
 * 加载主题资源
 */
function reddit_clone_scripts() {
    // 只加载 Tailwind CSS
    wp_enqueue_style('tailwind', get_template_directory_uri() . '/assets/css/tailwind.min.css');
    
    // 加载主题 JS
    wp_enqueue_script('reddit-clone', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), REDDIT_CLONE_VERSION, true);
    
    // 添加 AJAX 支持
    wp_localize_script('reddit-clone', 'redditClone', array(
        'ajaxurl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('reddit_clone_vote')
    ));
}
add_action('wp_enqueue_scripts', 'reddit_clone_scripts'); 